/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;

namespace ArtNotes
{


	public class ANImageMetaData: UIScrollView
	{

		private ANDatabase anDB;
		private Image image;
		
		public ANImageMetaData (Image img)
		{
			this.image = img;
			anDB = AN_APP_OPTIONS.AN_DATABASE;
			
			this.ContentSize = new SizeF(320f,470f);
			this.Frame = new RectangleF (0, 0, 320, 350);
			this.ClipsToBounds = true;
			this.ScrollEnabled = true;
			this.AlwaysBounceVertical = true;
			this.Bounds = this.Frame;
			this.ShowsVerticalScrollIndicator = true;
			this.DirectionalLockEnabled = true;
			
			
			this.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("Images/notepaper.png"));
			
			this.addEditingInfo();
		}
			
		private UITextField tfTitle;
		private UITextField tfAuthor;
		private UITextField tfLattitude;
		private UITextField tfLongitude;
		private UITextView tvDescription;
		private UILabel lTitle;
		private UILabel lAuthor;
		private UILabel lLattitude;
		private UILabel lLongitude;
		private UILabel lDescription;
		private UIImageView tbTitle;
		private UIImageView tbAuthor;
		private UIImageView tbLattitude;
		private UIImageView tbLongitude;
		private UIImageView taDescription;
		
		private void addEditingInfo()
		{
			//tb (20, 20, 180, 30)
			//i  (10, 5,  200, 55)
			
			UILabel title = new UILabel(new RectangleF(100,5,120,40));
			title.Text = "Details";
			title.BackgroundColor = UIColor.Clear;
			title.Font = UIFont.FromName("Marker Felt", 40);
			this.AddSubview(title);
			
			lTitle = createLabel("Title",new PointF(10,55),new SizeF(50,20));
			tbTitle = createTextBoxOutline(new PointF(120,45),new SizeF(200,40));
			tfTitle = createInputItem(new PointF(130,55));
			if(image.Title != null)
				tfTitle.Text = image.Title;
			
			lAuthor = createLabel("Author",new PointF(10,85),new SizeF(60,20));
			tbAuthor = createTextBoxOutline(new PointF(120,75),new SizeF(200,40));
			tfAuthor = createInputItem(new PointF(130,85));
			if(image.Author != null)
				tfAuthor.Text = image.Author;
			
			lDescription = createLabel("Description",new PointF(10,110),new SizeF(100,20));
			taDescription = new UIImageView(new RectangleF(new PointF(10,130),new SizeF(300,110)));
			taDescription.Image = UIImage.FromFile("Images/text_area_grey.png");
			taDescription.BackgroundColor = UIColor.Clear;
			//taDescription = createTextBoxOutline(new PointF(10,90),new SizeF(200,40));
			tvDescription = new UITextView();
			tvDescription.Frame = new RectangleF(20,140,280,90);
			tvDescription.BackgroundColor = UIColor.Clear;
			tvDescription.Font = UIFont.FromName ("Marker Felt", 18);
			tvDescription.TextColor = UIColor.DarkGray;
			if(image.Description != null)
				tvDescription.Text = image.Description;
			
			// The stupidity is strong in this one.
			tvDescription.Started += delegate {
				PointF dmf = this.ContentOffset;
				UIButton dismissItem = ImageButtonType(new RectangleF(260,dmf.Y + 10,55,58), UIImage.FromFile("Images/close_x.png"),UIImage.FromFile("Images/close_x.png"));
				dismissItem.TouchDown += delegate {
					//Console.WriteLine(tvDescription.Text);
					tvDescription.ResignFirstResponder ();
					dismissItem.RemoveFromSuperview();	
					dismissItem = null;
				};
			
				this.AddSubview(dismissItem);
			};
			
			lLongitude = createLabel("Longitude",new PointF(10,250),new SizeF(80,20));
			tbLongitude = createTextBoxOutline(new PointF(120,240),new SizeF(200,40));
			tfLongitude = createInputItem(new PointF(130,250));
			if(!image.Longitude.Equals(null))
				tfLongitude.Text = image.Longitude.ToString();
			
			lLattitude = createLabel("Lattitude",new PointF(10,280),new SizeF(80,20));
			tbLattitude = createTextBoxOutline(new PointF(120,270),new SizeF(200,40));
			tfLattitude = createInputItem(new PointF(130,280));
			if(!image.Lattitude.Equals(null))
				tfLattitude.Text = image.Lattitude.ToString();

			this.AddSubview(tbTitle);
			this.AddSubview(taDescription);
			this.AddSubview(tbAuthor);
			this.AddSubview(tbLattitude);
			this.AddSubview(tbLongitude);
			
			this.AddSubview(tfTitle);
			this.AddSubview(tvDescription);
			this.AddSubview(tfAuthor);
			this.AddSubview(tfLattitude);
			this.AddSubview(tfLongitude);
			
			this.AddSubview(lTitle);
			this.AddSubview(lDescription);
			this.AddSubview(lAuthor);
			this.AddSubview(lLattitude);
			this.AddSubview(lLongitude);
		}
		
		private UIButton ImageButtonType (RectangleF frame, UIImage defaultImage, UIImage pressedImage)
		{
			var background = defaultImage;//UIImage.FromFile("Images/btnSearch.png");
			var backgroundPressed = pressedImage;//UIImage.FromFile("Images/btnSearchPressed.png");
			//var frame = frame;
			
			var button = new UIButton(frame);
			var nbackground = background.StretchableImage(12,0);
			var nbackgroundPressed = backgroundPressed.StretchableImage(12,0);
			button.SetBackgroundImage(nbackground,UIControlState.Normal);
			button.SetBackgroundImage(nbackgroundPressed,UIControlState.Highlighted);

			button.Tag = 1;
			return button;
		}
		
		private UIButton RoundedButtonType (float xpos, float ypos, string btitle)
		{
			var button = UIButton.FromType (UIButtonType.RoundedRect);
			button.Frame = new RectangleF (xpos, ypos, 106f, 40f);
			button.BackgroundColor = UIColor.Clear;
			button.SetTitle (btitle, UIControlState.Normal);
			button.SetTitleColor (UIColor.Black, UIControlState.Normal);

			button.Tag = 1;
			// To support reusable cells
			return button;
		}
		
		private UIImageView createTextBoxOutline(PointF location, SizeF size)
		{
			UIImageView tmp = new UIImageView(new RectangleF(location,size));
			tmp.Image = UIImage.FromFile("Images/text_box_grey.png");
			tmp.BackgroundColor = UIColor.Clear;
			return tmp;
		}
		
		private UILabel createLabel(string title, PointF location, SizeF size)
		{
			UILabel tmp = new UILabel(new RectangleF(location,size));
			tmp.Font = UIFont.FromName ("Marker Felt", 20);
			tmp.BackgroundColor = UIColor.Clear;
			tmp.Text = title;
			return tmp;
		}
		
		private UITextField createInputItem(PointF location)
		{
			UITextField tmp = new UITextField(new RectangleF (location,new SizeF(180f,25f)));
			tmp.BorderStyle = UITextBorderStyle.None;//RoundedRect;
			tmp.BackgroundColor = UIColor.Clear;
			tmp.Font = UIFont.FromName ("Marker Felt", 18);
			tmp.TextColor = UIColor.DarkGray;
			tmp.ShouldReturn = editFieldInputHandler;
			return tmp;
		}
		
		bool editFieldInputHandler(UITextField tf)
		{
			tf.ResignFirstResponder();
			return true;
		}
		
		public void saveChanges()
		{
			double tmpCoord = 0;
			
			if(!tfTitle.Text.Equals(""))
				image.Title = tfTitle.Text;
			
			try{
				tmpCoord = Double.Parse(tfLongitude.Text);
				image.Longitude = tmpCoord;
				tmpCoord = Double.Parse(tfLattitude.Text);
				image.Lattitude = tmpCoord;
			}
			catch(Exception e)
			{}
			
			image.Author = tfAuthor.Text;
			image.Description = tvDescription.Text;
			
			anDB.Update("Image",image);
		}
	}
}
